<?php
declare (strict_types=1);

namespace app\command;

use app\model\Goods;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class DbExec extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('exec')
            ->setDescription('执行数据库迁移命令');
    }

    protected function execute(Input $input, Output $output)
    {
        $goods = Goods::with(["getCategory"])->select();
        $docs = [];
        for ($i = 0; $i < count($goods); $i++) {
            if ($goods[$i]['getCategory']){
                $docs[] = [
                    '产品名' => $goods[$i]['goods_name'],
                    '分类名称' => $goods[$i]['getCategory']['cat_name']
                ];
            }
        }
        file_put_contents('original_data.json', json_encode($docs, JSON_UNESCAPED_UNICODE));
        $output->writeln('已完成');
    }
}
